package org.jeecg.modules.dzswj.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.basesource.service.IDjNsrzyxxService;
import org.jeecg.modules.basesource.service.IDjPzjgxxService;
import org.jeecg.modules.basesource.service.ISbCwbbService;
import org.jeecg.modules.dzswj.DESUtil;
import org.jeecg.modules.dzswj.GuangdongSfjrApiService;
import org.jeecg.modules.front.vo.AuthReqVo;
import org.jeecg.modules.dzswj.vo.LoginSessionVo;
import org.jeecg.modules.dzswj.vo.request.*;
import org.jeecg.modules.dzswj.vo.response.DzswjBaseResponse;
import org.jeecg.modules.dzswj.vo.response.PzjgxxVo;
import org.jeecg.modules.dzswj.vo.response.SmzdlResponseVo;
import org.jeecg.modules.dzswj.vo.response.cwbb.GsCwbbVo;
import org.jeecg.modules.dzswj.vo.response.nsrxxcx.Nsrjbxx;
import org.jeecg.modules.dzswj.vo.response.nsrxxcx.NsrxxCxResponseVo;
import org.jeecg.modules.fk.entity.DjOrg;
import org.jeecg.modules.fk.service.IDjOrgService;
import org.jeecg.modules.qyxx.entity.*;
import org.jeecg.modules.qyxx.service.*;
import org.jeecg.modules.syncdata.service.SyncService;
import org.jeecg.modules.thirdpartyapi.constant.BgApiEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author chensherun on 2022/2/24.
 * @version 1.0
 */
@Api(tags = "三方接口测试", value = "三方接口测试")
@RestController
@RequestMapping("/test/sfjr")
@Profile({"local", "dev", "pre"})
public class SfjrTestController {

    @Autowired
    private GuangdongSfjrApiService sfjrApiService;

    @Autowired
    private IDjOrgService djOrgService;

    @Autowired
    private IDjNsrxxService djNsrxxService;

    @Autowired
    private IDjNsrzyxxService djNsrzyxxService;

    @Autowired
    private IDjPzjgxxService djPzjgxxService;

    @Autowired
    private ISbCwbbService sbCwbbService;

    @Autowired
    private SyncService syncService;

    @Autowired
    private IBgFpHzNdSygysService ndSygysService;
    @Autowired
    private IBgFpHzNdXyjxsService ndXyjxsService;
    @Autowired
    private IBgFpTopSygysService sygysService;
    @Autowired
    private IBgFpTopXyjxsService xyjxsService;
    @Autowired
    private IBgFpTopSyyclService syyclService;
    @Autowired
    private IBgFpTopXycpService xycpService;
    @Autowired
    private IBgFpHzNdWdxSxygysjxsService sxygysjxsService;
    @Autowired
    private IBgFpHzNdWdxSxyyclcpService sxyyclcpService;

    @ApiOperation(value = "原材料产品稳定性")
    @PostMapping("/testBgFpHzNdWdxSxyyclcp")
    public Result<?> testBgFpHzNdWdxSxyyclcp(String shxydm,String nsrmc, Integer tjnd) {
        BgFpHzNdWdxSxyyclcp sxygysjxs = new BgFpHzNdWdxSxyyclcp();
        LambdaQueryWrapper<BgFpTopSyycl> sygysQuery1 = Wrappers.lambdaQuery(BgFpTopSyycl.class);
        sygysQuery1.eq(BgFpTopSyycl::getDjxh,shxydm).eq(BgFpTopSyycl::getTjnd,tjnd).orderByDesc(BgFpTopSyycl::getFpje);
        List<BgFpTopSyycl> sygysList1 = syyclService.list(sygysQuery1);

        LambdaQueryWrapper<BgFpTopSyycl> sygysQuery2 = Wrappers.lambdaQuery(BgFpTopSyycl.class);
        sygysQuery2.eq(BgFpTopSyycl::getDjxh,shxydm).eq(BgFpTopSyycl::getTjnd,tjnd -1).orderByDesc(BgFpTopSyycl::getFpje);
        List<BgFpTopSyycl> sygysList2 = syyclService.list(sygysQuery2);
        int lssSize1 = sygysList2.size()-sygysList1.size() > 0 ? sygysList2.size()-sygysList1.size() : 0;
        int lsgysslCount = lssSize1; // 供应商流失数
        int count1 = 0;
        for (BgFpTopSyycl sygys1 : sygysList1) {
            int count2 = 0;
            count1++;
            String xsfmc1 = sygys1.getYclmc();
            boolean flag = true;
            for (BgFpTopSyycl sygys2 : sygysList2) {
                count2++;
                String xsfmc2 = sygys2.getYclmc();
                if (xsfmc1.equals(xsfmc2)){
                    flag = false;
                    break;
                }
                if (count2 == 5){
                    break;
                }
            }
            if (flag){
                lsgysslCount++;
            }
            if (count1 == 5){
                break;
            }
        }
        BigDecimal xzgyssl =null;
        if (sygysList2 == null || sygysList2.isEmpty()){
            lsgysslCount = 0;
            xzgyssl = BigDecimal.valueOf(sygysList1.size());
        }
        LambdaQueryWrapper<BgFpHzNdWdxSxyyclcp> query = Wrappers.lambdaQuery(BgFpHzNdWdxSxyyclcp.class);
        query.eq(BgFpHzNdWdxSxyyclcp::getDjxh,shxydm).eq(BgFpHzNdWdxSxyyclcp::getTjnd,tjnd - 1);
        BgFpHzNdWdxSxyyclcp qnSxygysjxs = sxyyclcpService.getOne(query);
        if (qnSxygysjxs == null){
            qnSxygysjxs = new BgFpHzNdWdxSxyyclcp();
        }
        BigDecimal qnLsgyssl = qnSxygysjxs.getLsyclsl(); // 去年流失供应商数量
        BigDecimal lsgyssl = BigDecimal.valueOf(lsgysslCount);
        BigDecimal lsgysslTb =null;
        if (qnLsgyssl != null && qnLsgyssl.compareTo(BigDecimal.ZERO) != 0){
            lsgysslTb = lsgyssl.subtract(qnLsgyssl).divide(qnLsgyssl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 流失供应商数量同比（%）
        }
        if (xzgyssl == null){
            xzgyssl = lsgyssl;
        }
        BigDecimal gysLcs = new BigDecimal(5).subtract(lsgyssl); // 供应商留存数
        BigDecimal gyslcl = gysLcs.divide(new BigDecimal(5),4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));  // 供应商留存率
        BigDecimal qngyslcl = qnSxygysjxs.getYcllcl(); // 去年供应商留存率
        BigDecimal gyslclTb = null;
        if (qngyslcl != null && qngyslcl.compareTo(BigDecimal.ZERO) != 0) {
            gyslclTb = gyslcl.subtract(qngyslcl).divide(qngyslcl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
        } // 供应商留存率同比（%）
        sxygysjxs.setDjxh(shxydm);
        sxygysjxs.setLsyclsl(lsgyssl);
        sxygysjxs.setLsyclslTb(lsgysslTb);
        sxygysjxs.setYcllcl(gyslcl);
        sxygysjxs.setYcllclTb(gyslclTb);

        LambdaQueryWrapper<BgFpTopXyjxs> xyjxsQuery1 = Wrappers.lambdaQuery(BgFpTopXyjxs.class);
        xyjxsQuery1.eq(BgFpTopXyjxs::getDjxh,shxydm).eq(BgFpTopXyjxs::getTjnd,tjnd).orderByDesc(BgFpTopXyjxs::getFpje);
        List<BgFpTopXyjxs> xyjxsList1 = xyjxsService.list(xyjxsQuery1);

        LambdaQueryWrapper<BgFpTopXyjxs> xyjxsQuery2 = Wrappers.lambdaQuery(BgFpTopXyjxs.class);
        xyjxsQuery2.eq(BgFpTopXyjxs::getDjxh,shxydm).eq(BgFpTopXyjxs::getTjnd,tjnd - 1).orderByDesc(BgFpTopXyjxs::getFpje);
        List<BgFpTopXyjxs> xyjxsList2 = xyjxsService.list(xyjxsQuery2);
        int lssSize2 = xyjxsList2.size()-xyjxsList1.size() > 0 ? xyjxsList2.size()-xyjxsList1.size() : 0; // 如果数量不足10,10-size = ?,提前流失了的数量
        BigDecimal lsjxssl = BigDecimal.valueOf(lssSize2); // 供应商流失数
        int count3 = 0;
        for (BgFpTopXyjxs xyjxs1 : xyjxsList1) {
            count3++;
            int count4 = 0;
            String gmfmc1 = xyjxs1.getGmfmc();
            boolean flag = true;
            for (BgFpTopXyjxs xyjxs2 : xyjxsList2) {
                count4++;
                String gmfmc2 = xyjxs2.getGmfmc();
                if (gmfmc1.equals(gmfmc2)){
                    flag = false;
                    break;
                }
                if (count4 ==5){
                    break;
                }
            }
            if (flag){
                lsjxssl = lsjxssl.add(BigDecimal.ONE);
            }
            if (count3 ==5){
                break;
            }
        }
        BigDecimal xzjxssl =null;
        if (xyjxsList2 == null || xyjxsList2.isEmpty()){
            lsjxssl = BigDecimal.ZERO;
            xzjxssl = BigDecimal.valueOf(xyjxsList1.size());
        }
        BigDecimal qnLsjxssl = qnSxygysjxs.getLscpsl(); // 去年流失经销商数量
        BigDecimal lsjxsslTb =null;
        if (qnLsjxssl != null && qnLsjxssl.compareTo(BigDecimal.ZERO) != 0){
            lsjxsslTb = lsjxssl.subtract(qnLsjxssl).divide(qnLsjxssl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 流失经销商数量同比（%）
        }
        if (xzjxssl == null){
            xzjxssl = lsjxssl;
        }
        BigDecimal jxsLcs = new BigDecimal(5).subtract(lsjxssl); // 经销商留存数
        BigDecimal jxslcl = jxsLcs.divide(new BigDecimal(5),4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 经销商留存率
        BigDecimal qnJxslcl = qnSxygysjxs.getCplcl(); // 去年经销商留存率
        BigDecimal jxslclTb = null;
        if (qnJxslcl != null && qnJxslcl.compareTo(BigDecimal.ZERO) != 0){
            jxslclTb = jxslcl.subtract(qnJxslcl).divide(qnJxslcl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 经销商留存率同比（%）
        }
        sxygysjxs.setLscpsl(lsjxssl);
        sxygysjxs.setLscpslTb(lsjxsslTb);
        sxygysjxs.setCplcl(jxslcl);
        sxygysjxs.setCplclTb(jxslclTb);
        sxygysjxs.setTjnd(tjnd.toString());
        sxygysjxs.setNsrmc(nsrmc);
        sxyyclcpService.saveOrUpdateByMultiId(sxygysjxs);
        return Result.OK(sxygysjxs);
    }

    @ApiOperation(value = "上下游供应商稳定性")
    @PostMapping("/testBgFpHzNdWdxSxygysjxs")
    public Result<?> testBgFpHzNdWdxSxygysjxs(String shxydm,String nsrmc, Integer tjnd) {
        BgFpHzNdWdxSxygysjxs sxygysjxs = new BgFpHzNdWdxSxygysjxs();
        LambdaQueryWrapper<BgFpTopSygys> sygysQuery1 = Wrappers.lambdaQuery(BgFpTopSygys.class);
        sygysQuery1.eq(BgFpTopSygys::getDjxh,shxydm).eq(BgFpTopSygys::getTjnd,tjnd);
        List<BgFpTopSygys> sygysList1 = sygysService.list(sygysQuery1);

        LambdaQueryWrapper<BgFpTopSygys> sygysQuery2 = Wrappers.lambdaQuery(BgFpTopSygys.class);
        sygysQuery2.eq(BgFpTopSygys::getDjxh,shxydm).eq(BgFpTopSygys::getTjnd,tjnd -1);
        List<BgFpTopSygys> sygysList2 = sygysService.list(sygysQuery2);
        int lsgysslCount = sygysList2.size()-sygysList1.size()>0 ? sygysList2.size()-sygysList1.size() : 0; // 供应商流失数
        for (BgFpTopSygys sygys1 : sygysList1) {
            String xsfmc1 = sygys1.getXsfmc();
            boolean flag = true;
            for (BgFpTopSygys sygys2 : sygysList2) {
                String xsfmc2 = sygys2.getXsfmc();
                if (xsfmc1.equals(xsfmc2)){
                    flag = false;
                    break;
                }
            }
            if (flag){
                lsgysslCount++;
            }
        }
        BigDecimal xzgyssl =null;
        if (sygysList2 == null || sygysList2.isEmpty()){
            lsgysslCount = 0;
            xzgyssl = BigDecimal.valueOf(sygysList1.size());
        }
        LambdaQueryWrapper<BgFpHzNdWdxSxygysjxs> query = Wrappers.lambdaQuery(BgFpHzNdWdxSxygysjxs.class);
        query.eq(BgFpHzNdWdxSxygysjxs::getDjxh,shxydm).eq(BgFpHzNdWdxSxygysjxs::getTjnd,tjnd - 1);
        BgFpHzNdWdxSxygysjxs qnSxygysjxs = sxygysjxsService.getOne(query);
        if (qnSxygysjxs == null){
            qnSxygysjxs = new BgFpHzNdWdxSxygysjxs();
        }
        BigDecimal qnLsgyssl = qnSxygysjxs.getLsgyssl(); // 去年流失供应商数量
        BigDecimal lsgyssl = BigDecimal.valueOf(lsgysslCount);
        BigDecimal lsgysslTb =null;
        if (qnLsgyssl != null && qnLsgyssl.compareTo(BigDecimal.ZERO) != 0){
            lsgysslTb = lsgyssl.subtract(qnLsgyssl).divide(qnLsgyssl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 流失供应商数量同比（%）
        }
        if (xzgyssl == null){
            xzgyssl = lsgyssl;
        }
        BigDecimal qnXzgyssl = qnSxygysjxs.getXzgyssl();
        BigDecimal xzgysslTb = null;
        if (qnLsgyssl != null && qnXzgyssl.compareTo(BigDecimal.ZERO) != 0){
            xzgysslTb = xzgyssl.subtract(qnXzgyssl).divide(qnXzgyssl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 新增供应商数量同比（%）
        }
        BigDecimal gysLcs = BigDecimal.TEN.subtract(lsgyssl); // 供应商留存数
        BigDecimal gyslcl = gysLcs.divide(BigDecimal.TEN,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));  // 供应商留存率
        BigDecimal qngyslcl = qnSxygysjxs.getGyslcl(); // 去年供应商留存率
        BigDecimal gyslclTb = null;
        if (qngyslcl != null && qngyslcl.compareTo(BigDecimal.ZERO) != 0) {
             gyslclTb = gyslcl.subtract(qngyslcl).divide(qngyslcl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
        } // 供应商留存率同比（%）
        sxygysjxs.setDjxh(shxydm);
        sxygysjxs.setLsgyssl(lsgyssl);
        sxygysjxs.setLsgysslTb(lsgysslTb);
        sxygysjxs.setXzgyssl(xzgyssl);
        sxygysjxs.setXzgysslTb(xzgysslTb);
        sxygysjxs.setGyslcl(gyslcl);
        sxygysjxs.setGyslclTb(gyslclTb);

        LambdaQueryWrapper<BgFpTopXyjxs> xyjxsQuery1 = Wrappers.lambdaQuery(BgFpTopXyjxs.class);
        xyjxsQuery1.eq(BgFpTopXyjxs::getDjxh,shxydm).eq(BgFpTopXyjxs::getTjnd,tjnd);
        List<BgFpTopXyjxs> xyjxsList1 = xyjxsService.list(xyjxsQuery1);

        LambdaQueryWrapper<BgFpTopXyjxs> xyjxsQuery2 = Wrappers.lambdaQuery(BgFpTopXyjxs.class);
        xyjxsQuery2.eq(BgFpTopXyjxs::getDjxh,shxydm).eq(BgFpTopXyjxs::getTjnd,tjnd - 1);
        List<BgFpTopXyjxs> xyjxsList2 = xyjxsService.list(xyjxsQuery2);
        int lssSize2 = xyjxsList2.size()-xyjxsList1.size()>0?xyjxsList2.size()-xyjxsList1.size():0;
        BigDecimal lsjxssl = BigDecimal.valueOf(lssSize2); // 供应商流失数
        for (BgFpTopXyjxs xyjxs1 : xyjxsList1) {
            String gmfmc1 = xyjxs1.getGmfmc();
            boolean flag = true;
            for (BgFpTopXyjxs xyjxs2 : xyjxsList2) {
                String gmfmc2 = xyjxs2.getGmfmc();
                if (gmfmc1.equals(gmfmc2)){
                    flag = false;
                    break;
                }
            }
            if (flag){
                lsjxssl = lsjxssl.add(BigDecimal.ONE);
            }
        }
        BigDecimal xzjxssl =null;
        if (xyjxsList2 == null || xyjxsList2.isEmpty()){
            lsjxssl = BigDecimal.ZERO;
            xzjxssl = BigDecimal.valueOf(xyjxsList1.size());
        }
        BigDecimal qnLsjxssl = qnSxygysjxs.getLsjxssl(); // 去年流失经销商数量
        BigDecimal lsjxsslTb =null;
        if (qnLsjxssl != null && qnLsjxssl.compareTo(BigDecimal.ZERO) != 0){
            lsjxsslTb = lsjxssl.subtract(qnLsjxssl).divide(qnLsjxssl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 流失经销商数量同比（%）
        }
        if (xzjxssl == null){
            xzjxssl = lsjxssl;
        }
        BigDecimal qnXzjxssl = qnSxygysjxs.getXzjxssl();
        BigDecimal xzjxsslTb = null;
        if (qnXzjxssl != null && qnXzjxssl.compareTo(BigDecimal.ZERO) != 0){
            xzjxsslTb = xzjxssl.subtract(qnXzjxssl).divide(qnXzjxssl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 新增经销商数量同比（%）
        }
        BigDecimal jxsLcs = BigDecimal.TEN.subtract(lsjxssl); // 经销商留存数
        BigDecimal jxslcl = jxsLcs.divide(BigDecimal.TEN,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 经销商留存率
        BigDecimal qnJxslcl = qnSxygysjxs.getJxslcl(); // 去年经销商留存率
        BigDecimal jxslclTb = null;
        if (qnJxslcl != null && qnJxslcl.compareTo(BigDecimal.ZERO) != 0){
            jxslclTb = jxslcl.subtract(qnJxslcl).divide(qnJxslcl,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)); // 经销商留存率同比（%）
        }
        sxygysjxs.setLsjxssl(lsjxssl);
        sxygysjxs.setLsjxsslTb(lsjxsslTb);
        sxygysjxs.setXzjxssl(xzjxssl);
        sxygysjxs.setXzjxsslTb(xzjxsslTb);
        sxygysjxs.setJxslcl(jxslcl);
        sxygysjxs.setJxslclTb(jxslclTb);
        sxygysjxs.setTjnd(tjnd.toString());
        sxygysjxs.setNsrmc(nsrmc);
        sxygysjxsService.saveOrUpdateByMultiId(sxygysjxs);
        return Result.OK(sxygysjxs);
    }

    @ApiOperation(value = "计算占比(临时)")
    @PostMapping("/testCalculatedProportion")
    public Result<?> testCalculatedProportion(String shxydm) {
        LambdaQueryWrapper<BgFpHzNdSygys> gysjxsQuery = Wrappers.lambdaQuery(BgFpHzNdSygys.class);
        gysjxsQuery.eq(BgFpHzNdSygys::getDjxh,shxydm);
        List<BgFpHzNdSygys> ndSygysList = ndSygysService.list(gysjxsQuery);
        for (BgFpHzNdSygys ndSygys : ndSygysList) {
            String tjnd = ndSygys.getTjnd();
            BigDecimal gysFpje = ndSygys.getGysFpje();
            if (gysFpje != null && gysFpje.compareTo(BigDecimal.ZERO) != 0){
                LambdaQueryWrapper<BgFpTopSygys> sygysQuery = Wrappers.lambdaQuery(BgFpTopSygys.class);
                sygysQuery.eq(BgFpTopSygys::getDjxh,shxydm).eq(BgFpTopSygys::getTjnd,tjnd);
                List<BgFpTopSygys> sygysList = sygysService.list(sygysQuery);
                for (BgFpTopSygys sygys : sygysList) {
                    BigDecimal fpje = sygys.getFpje();
                    if (fpje != null){
                        BigDecimal jezb = fpje.divide(gysFpje, 4, BigDecimal.ROUND_HALF_UP);
                        sygys.setJezb(jezb.multiply(new BigDecimal("100")));
                    }
                }
                sygysService.updateBatchByMultiId(sygysList);

                LambdaQueryWrapper<BgFpTopSyycl> yclQuery = Wrappers.lambdaQuery(BgFpTopSyycl.class);
                yclQuery.eq(BgFpTopSyycl::getDjxh,shxydm).eq(BgFpTopSyycl::getTjnd,tjnd);
                List<BgFpTopSyycl> syyclList = syyclService.list(yclQuery);
                for (BgFpTopSyycl syycl : syyclList) {
                    BigDecimal fpje = syycl.getFpje();
                    if (fpje != null){
                        BigDecimal jezb = fpje.divide(gysFpje, 4, BigDecimal.ROUND_HALF_UP);
                        syycl.setJezb(jezb.multiply(new BigDecimal("100")));
                    }
                }
                syyclService.updateBatchByMultiId(syyclList);
            }
        }
        LambdaQueryWrapper<BgFpHzNdXyjxs> yclcpQuery = Wrappers.lambdaQuery(BgFpHzNdXyjxs.class);
        yclcpQuery.eq(BgFpHzNdXyjxs::getDjxh,shxydm);
        List<BgFpHzNdXyjxs> sxyyclcpList = ndXyjxsService.list(yclcpQuery);
        for (BgFpHzNdXyjxs sxyyclcp : sxyyclcpList) {
            String tjnd = sxyyclcp.getTjnd();
            BigDecimal jxsFpje = sxyyclcp.getJxsFpje();
            if (jxsFpje != null && jxsFpje.compareTo(BigDecimal.ZERO) != 0){
                LambdaQueryWrapper<BgFpTopXyjxs> xyjxsQuery = Wrappers.lambdaQuery(BgFpTopXyjxs.class);
                xyjxsQuery.eq(BgFpTopXyjxs::getDjxh,shxydm).eq(BgFpTopXyjxs::getTjnd,tjnd);
                List<BgFpTopXyjxs> xyjxsList = xyjxsService.list(xyjxsQuery);
                for (BgFpTopXyjxs xyjxs : xyjxsList) {
                    BigDecimal fpje = xyjxs.getFpje();
                    if (fpje != null){
                        BigDecimal jezb = fpje.divide(jxsFpje, 4, BigDecimal.ROUND_HALF_UP);
                        xyjxs.setJezb(jezb.multiply(new BigDecimal("100")));
                    }
                }
                xyjxsService.updateBatchByMultiId(xyjxsList);

                LambdaQueryWrapper<BgFpTopXycp> cpQuery = Wrappers.lambdaQuery(BgFpTopXycp.class);
                cpQuery.eq(BgFpTopXycp::getDjxh,shxydm).eq(BgFpTopXycp::getTjnd,tjnd);
                List<BgFpTopXycp> xycpList = xycpService.list(cpQuery);
                for (BgFpTopXycp xycp : xycpList) {
                    BigDecimal fpje = xycp.getFpje();
                    if (fpje != null){
                        BigDecimal jezb = fpje.divide(jxsFpje, 4, BigDecimal.ROUND_HALF_UP);
                        xycp.setJezb(jezb.multiply(new BigDecimal("100")));
                    }
                }
                xycpService.updateBatchByMultiId(xycpList);
            }
        }
        return Result.OK();
    }

    @ApiOperation(value = "同步目标表")
    @PostMapping("/syncTable")
    public Result<?> syncTable(String dayTime, BgApiEnum bgApiEnum) {
        ExecutorService executor = new ThreadPoolExecutor(5, 5, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
                new ThreadPoolExecutor.CallerRunsPolicy());
        DateTime dateTime = DateUtil.parseDate(dayTime);
        syncService.syncSingleTable(executor, bgApiEnum, dateTime);
        return Result.OK();
    }

    @ApiOperation(value = "实名登录")
    @PostMapping("/djyhmhmmdljk")
    public Result<?> djyhmhmmdljk(@RequestBody SmzyhdlReqVo reqVo) {
        SmzdlResponseVo.Body data = sfjrApiService.djyhmhmmdljk(reqVo);
        return Result.OK(data);
    }

    @ApiOperation(value = "选择企业身份")
    @PostMapping("/xzqysf")
    public Result<?> xzqysf(@RequestParam String sessionId, @RequestParam String swjgDm, @RequestParam String qybdid) {
        Boolean xzqysf = sfjrApiService.xzqysf(sessionId, swjgDm, qybdid);
        return Result.OK(xzqysf);
    }

    @ApiOperation(value = "查询纳税人基本信息")
    @PostMapping("/nsrxxCx")
    public Result<?> nsrxxCx(@RequestParam String sessionId, @RequestParam String swjgDm, @RequestParam String djxh) {
        NsrxxCxResponseVo data = sfjrApiService.nsrxxCx(sessionId, swjgDm, djxh);
        return Result.OK(data);
    }

    @ApiOperation(value = "查询批准机构信息")
    @PostMapping("/nsrxxPzjgxx")
    public Result<?> nsrxxPzjgxx(@RequestParam String sessionId, @RequestParam String swjgDm, @RequestParam String shxydm) {
        DzswjBaseResponse<List<PzjgxxVo>> data = sfjrApiService.nsrxxPzjgxx(sessionId, swjgDm, shxydm);
        return Result.OK(data);
    }

    @ApiOperation(value = "查询财务报表")
    @PostMapping("/cwbb")
    public Result<?> cwbb(@RequestParam String sessionId, @RequestParam String swjgDm, @RequestBody CwbbReqVo reqVo) {
        DjNsrxxPO djNsrxxPO = djNsrxxService.getByNsrsbhOrShxydm(reqVo.getTynsrsbh());
        reqVo.setDjxh(djNsrxxPO.getDjxh());
        GsCwbbVo cwbb = sfjrApiService.cwbb(sessionId, swjgDm, reqVo);
        if (cwbb == null) {
            //该期财报未申报

        }
        sbCwbbService.saveJbCwbb(djNsrxxPO.getTynsrsbh(), djNsrxxPO.getNsrmc(), reqVo.getSsqq(), reqVo.getSsqz(), cwbb);
        return Result.OK(cwbb);
    }

    @ApiOperation(value = "查询申报记录")
    @PostMapping("/cxnsrysbjl")
    public Result<?> cxnsrysbjl(@RequestParam String sessionId, @RequestParam String swjgDm, @RequestBody CxnsrysbjlReqVo reqVo) {
        return Result.OK(sfjrApiService.cxnsrysbjl(sessionId, swjgDm, reqVo));
    }

    @ApiOperation(value = "电子底账-发票详细信息查询（列表-不含货物明细）")
    @PostMapping("/dzdzplfpxxxxcx")
    public Result<?> dzdzplfpxxxxcx(@RequestParam String sessionId, @RequestParam String swjgDm, @RequestBody DzdzplFpxxxxcxReqVo reqVo) {
        return Result.OK(sfjrApiService.dzdzplfpxxxxcx(sessionId, swjgDm, reqVo));
    }

    @ApiOperation(value = "查询发票清单")
    @PostMapping("/dzdzfpqdcx")
    public Result<?> dzdzfpqdcx(@RequestParam String sessionId, @RequestParam String swjgDm, @RequestBody DzdzqdcxReqVo reqVo) {
        return Result.OK(sfjrApiService.dzdzfpqdcx(sessionId, swjgDm, reqVo));
    }

    @ApiOperation(value = "查询发票详细(主表和货物明细)")
    @PostMapping("/dzdzfpbccx")
    public Result<?> dzdzfpbccx(@RequestParam String sessionId, @RequestParam String swjgDm, @RequestBody DzdzfpbccxReqVo reqVo) {
        return Result.OK(sfjrApiService.dzdzfpbccx(sessionId, swjgDm, reqVo));
    }

    @ApiOperation(value = "获取企业的登录session")
    @PostMapping("/getNsrLoginSession")
    public Result<?> getNsrLoginSession(@RequestParam String tynsrsbh) {
        LoginSessionVo nsrLoginSessionInfo = sfjrApiService.getNsrLoginSessionInfo(tynsrsbh);
        return Result.OK(nsrLoginSessionInfo);
    }

    @ApiOperation(value = "模拟授权")
    @PostMapping("/auth")
    public Result<?> auth(@RequestBody AuthReqVo authVo) {
        //验证是否能登录
        SmzyhdlReqVo smzyhdlReqVo = new SmzyhdlReqVo();
        smzyhdlReqVo.setYhm(authVo.getYhm());
        smzyhdlReqVo.setPassword(authVo.getPassword());
        SmzdlResponseVo.Body djyhmhmmdljk = sfjrApiService.djyhmhmmdljk(smzyhdlReqVo);
        //判断是否存在dj_org (mysql和oracle)
        SmzdlResponseVo.SsoLoginInfo ssoLoginInfo = djyhmhmmdljk.getSsoLoginInfo();
        List<SmzdlResponseVo.Nsr> nsrs = ssoLoginInfo.getNsrs();
        //SmzdlResponseVo.Nsr nsr = null;
        //djxh list
        Set<String> djxhSet = new HashSet<>();
        for (SmzdlResponseVo.Nsr nsrVo : nsrs) {
            if ("N".equals(nsrVo.getTybz()) && (authVo.getNsrsbh().equals(nsrVo.getGsnsrsbh()) || authVo.getNsrsbh().equals(nsrVo.getShxydm()))) {
                //nsr = nsrVo;
                djxhSet.add(nsrVo.getGszdjxh());
            }
        }
        if (djxhSet.size() == 0) {
            throw new JeecgBootException("实名账号没有绑定企业");
        }
//        sfjrApiService.xzqysf(djyhmhmmdljk.getSsoSessionId(), null, nsr.getQybdid());
        DjNsrxxPO djNsrxx = djNsrxxService.getByNsrsbhOrShxydm(authVo.getNsrsbh());
        //纳税人表信息 dj_nsrxx, dj_nsrzyxx
        if (djNsrxx == null) {
            //查询电局接口添加
            NsrxxCxResponseVo nsrxxCxResponseVo = null;
            for (String djxh : djxhSet) {
                NsrxxCxResponseVo nsrxxCxResponseVoTemp = sfjrApiService.nsrxxCx(djyhmhmmdljk.getSsoSessionId(), null, djxh);
                Nsrjbxx nsrjbxx = nsrxxCxResponseVoTemp.getNsrjbxx();
                //只保存满足条件的djxh对应的企业信息
                if ("N".equals(nsrjbxx.getKqccsztdjbz()) && Integer.valueOf(nsrjbxx.getKzztdjlxdm()) <= 1110 && "Y".equals(nsrjbxx.getYxbz())) {
                    nsrxxCxResponseVo = nsrxxCxResponseVoTemp;
                    break;
                }
            }
            Nsrjbxx nsrjbxx = nsrxxCxResponseVo.getNsrjbxx();
            String tynsrsbh = StringUtils.isNotBlank(nsrjbxx.getShxyDm()) ? nsrjbxx.getShxyDm() : nsrjbxx.getNsrsbh();
            DzswjBaseResponse<List<PzjgxxVo>> pzjgxxResponse = sfjrApiService.nsrxxPzjgxx(djyhmhmmdljk.getSsoSessionId(), nsrjbxx.getZgswjdm(), tynsrsbh);
            List<PzjgxxVo> pzjgxxVoList = pzjgxxResponse.getBody();
            djNsrxx = djNsrxxService.saveOrUpdateNsrjbxxAndPzjg(nsrxxCxResponseVo, pzjgxxVoList);
        }
        //不存在则插入
        DjOrg djOrg = djOrgService.getByNsrsbhOrShxydm(authVo.getNsrsbh());
        //存在则更新
        if (djOrg == null) {
            djOrg = new DjOrg();
            djOrg.setOrgId(djNsrxx.getTynsrsbh());
            djOrg.setDjxh(djNsrxx.getTynsrsbh());
            djOrg.setNsrsbh(djNsrxx.getNsrsbh());
            djOrg.setShxydm(djNsrxx.getShxydm());
            djOrg.setNsrmc(djNsrxx.getNsrmc());
        }
        //授权人手机号，授权人名称， 授权电局账号，授权电局名称 TODO
        djOrg.setRealYhm(authVo.getYhm());
        String encryptPw = null;
        try {
            encryptPw = DESUtil.encrypt(authVo.getPassword(), GuangdongSfjrApiService.DB_PASS_KEY);
        } catch (Exception e) {
            //日志
            throw new JeecgBootException("系统异常");
        }
        djOrg.setPassword(encryptPw);
        djOrg.setAutherMobile("123456");
        djOrg.setAutherName("test");
        djOrgService.saveOrUpdate(djOrg);
        //djOrgService.updateById(djOrg);
        return Result.OK();
    }
}
